/*20/10/2012
 * A helper class which use to manipulate with database easier
 * 
 */
package Model.DataUltility;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import Model.ObjectBean.RequestBean.*;
/**
 *
 * @author chickencake1
 */
public class Data_Access {
    
    /*
     * Function: get all record from table 
     */
    public ResultSet getAllInformation(String table)
    {
        String query="select * from "+table;
        return DatabaseConnector.getResultSet(query);
    }
    
    /*
     * Function : get correct record from table
     * 
     */
    public ResultSet getInformation(String table,String []column,String [] data)
    {
        String query="select * from "+table+" where ";
       for(int i=0;i<column.length;i++)
       {
           query+=column[i]+"='"+data[i];
           if(i<column.length-1)
           {
               query+="' and ";
           }
           else
               query+="';";
       }
       System.out.println(query);
       return DatabaseConnector.getResultSet(query);       
    }
    
    /*Function : insert 1 record to table
     * 
     */
    public void insertInformation(String table,String []data)
    {
        String query="insert into "+table+"() values(N'";
        for(int i=0;i<data.length;i++)
        {
            query+=data[i];
            if(i<data.length-1)
                query+="',N'";
            else
                query+="');";
        }
        System.out.println(query);
        DatabaseConnector.executeQueryString(query);
    }
    
    /*Function : Insert into table have auto increment 
     * 
     * 
     */
    
        public void insertInformation_AutoIncre(String table,String []data)
    {
        String query="insert into "+table+"() values(0,N'";
        for(int i=0;i<data.length;i++)
        {
            query+=data[i];
            if(i<data.length-1)
                query+="',N'";
            else
                query+="');";
        }
        System.out.println(query);
        DatabaseConnector.executeQueryString(query);
    }
    
    /*Function: Update one or more record in a table in one or more column
     * 
     */
    
    public void updateInformation(String table,String []column,
            String []new_data,String []Condition_Col,String [] Condition_data
            ,String Condition_var)
    {
        String query="update "+table+" set ";
        for(int i=0;i<column.length;i++)
        {
            query+=column[i]+"='"+new_data[i];
            if(i<column.length-1)
                query+="',";
            else
                query+="' where ";
                           
        }
        
        if(Condition_Col.length>0)
                {
               
                for(int j=0;j<Condition_Col.length;j++)
                {
                    query+=Condition_Col[j]+" ='"+Condition_data[j];
                    if(j<Condition_Col.length-1)
                        query+="'"+Condition_var;
                    else
                        query+="';";
                }
                
                }
        System.out.println(query);
        DatabaseConnector.executeQueryString(query);
    }
    
    /*Function: Remove one or more record in a table
     * 
     */
    public void deleteInformation(String table,String []condition_col
            ,String []condition_data,String condition_var)
    {
        String query="delete from "+table+" where ";
        for(int i=0;i<condition_col.length;i++)
        {
            query+=condition_col[i]+" = '"+condition_data[i]+" ";
            if(i<condition_col.length-1)
                query+=condition_var;
            else
                query+="';";
        }
        System.out.println(query);
        DatabaseConnector.executeQueryString(query);
    }
    
    /*Function : make empty a table completely
     * 
     */
    public void deleAllInformation(String table)
    {
        String query="truncate "+table;
        DatabaseConnector.executeQueryString(query);
    }
    
    
    
    
    public static void main(String []argv)
    {
        Data_Access a= new Data_Access();
        
        
        /*String []Column={"id","passw"};
        String []new_data={"khanh","kim"};
        String []Condition_cold={"id"};
        String []Condition_data={"khang"};
        
        a.updateInformation("login_account", Column, new_data, Condition_cold, Condition_data,null);
        
        
        String []Condition_col1={"id"};
        String []Condition_data1={"khanh"};
        a.deleteInformation("login_account", Condition_col1, Condition_data1, null);
        
        String [] column={"id"};
        String [] data={"khanh"};
        ResultSet tets =a.getInformation("login_account",column,data);
        try {
            while(tets.next())
            {
                System.out.println(tets.getString("id"));
                System.out.println(tets.getString("passw"));
            }
        } catch (SQLException ex) {
            System.out.println("Fail");
            Logger.getLogger(Data_Access.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        Data_Access access=new Data_Access();
        BeanRegister register=new BeanRegister();
               String []personal_data={register.getUsername(),register.getFirstname(),register.getLastname(),
       register.getEmail(),register.getAddress(),register.getSex(),register.getPhonenumber(),
       register.getCity(),register.getBirthday(),register.getPayment_style(),register.getQuestion(),
       register.getAnswer()};
       
       //access.insertInformation("login_account", account_data);
       access.insertInformation("personal_info", personal_data);
       
    }
}
